package cn.tuning.controller;

import cn.tuning.entity.UserInfo;
import cn.tuning.pojo.APIResult;
import cn.tuning.service.UserService;
import cn.tuning.utils.IdWorkerUtil;
import cn.tuning.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


/**
 * @author lixiaolong
 * @date 2021-08-06 22:01
 * @description:
 */

@RestController
@ResponseBody
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MailUtil mailUtil;

    /**
     * 登录
     * @param userInfo
     * @param request
     * @return
     */
    @PostMapping("/login")
    public APIResult login( UserInfo userInfo, HttpServletRequest request){
        APIResult apiResult = null;
        String username = userInfo.getUsername();
        String password = userInfo.getPassword();
        //判断是否为null
        if (username == null || password ==null){
            apiResult = APIResult.massageMiss();
            //返回邮箱不存在
            return apiResult;
        }
        apiResult = userService.login(userInfo);
        if (apiResult.getCode() == 200){
            //存储session
            request.getSession().setAttribute(username,username);
        }
        return apiResult;
    }

    /**
     * 注册
     * @param userInfo
     * @return
     */
    @PostMapping("/register")
    public APIResult register( UserInfo userInfo,
                              @RequestParam(value = "inputCode" ,required = false) String inputCode,
                              HttpServletRequest request){
        APIResult apiResult = null; //判断是否为null
        if (Objects.isNull(userInfo.getUsername()) || Objects.isNull(userInfo.getPassword()) || inputCode == null){
                apiResult = APIResult.errorRegister(); // 注册失败
            return apiResult;
        }
        if(Objects.isNull( request.getSession().getAttribute(userInfo.getUsername()+inputCode))){//判断验证码
            apiResult = APIResult.errorVerify();//验证码错误 : 400
            return apiResult;
        }
        IdWorkerUtil worker = new IdWorkerUtil(1,1,1);
        String uid= String.valueOf(worker.nextId());
        apiResult = userService.register(userInfo);
        return apiResult;
    }

    /**
     * 验证码发送
     * @param request
     * @return
     */
    @PostMapping("/verify")
    public APIResult verify(@RequestParam(value = "username",required = false) String username,
                            HttpServletRequest request){
        APIResult apiResult = null;//判断是否为null
        if (username == null){
            apiResult = APIResult.nullEmail();//邮箱不能为空 :400
            return apiResult;
        }
        //邮箱格式判断
        String pattern = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        if(!username.matches(pattern)){
              apiResult = APIResult.errorMail(); //邮箱格式错误请输入正确的邮箱 : 400
              return apiResult;
        }
        apiResult = userService.verify(username); //查看邮箱是否存在
        if ( apiResult.getCode() != 401) {
            String verificationCode = mailUtil.sentEmail(username);//发送邮件
            request.getSession().setAttribute(username + verificationCode, verificationCode);
            return apiResult; //SUCCESS 200
        }
        return apiResult;//邮箱已存在 401
    }

    /**
     * 注销
     * @param userInfo
     * @return
     */
    @GetMapping("logout")
    public APIResult logout( UserInfo userInfo, HttpServletRequest request){
        APIResult apiResult = null;
        String username = userInfo.getUsername();
        if (username == null){//判断是否为null
            apiResult = APIResult.errorOperate();
            return apiResult;//操作失败,请求操作的资源不存在 : 405
        }
        request.getSession().removeAttribute(username);//清除session
        apiResult = APIResult.success();
        return apiResult; //SUCCESS : 200
    }
}
